package com.harzone.commons.search.domain;

import java.util.ArrayList;
import java.util.List;

import com.harzone.commons.search.utils.SearchUtil;

/**
 * 查询条件函数
 * @author ls
 * 2014年11月21日下午2:33:25
 */
public class MethodCondition extends Where implements Cloneable{
	
	//参数
	private List<KeyValue> params = new ArrayList<KeyValue>();
	//条件
	private String option;

	protected String name = "";
	
	protected Object value = null;

	protected MethodOPEAR opear = MethodOPEAR.EQ;

	/**
	 * 操作符
	 * @author ls
	 * 2014年11月21日下午2:24:06
	 */
	public static enum MethodOPEAR {
		EQ("=","等于")
		,EQ1("==","等于")
		, GT(">","大于")
		, LT("<","小于")
		, GTE(">=","大于等于")
		, LTE("<=","小于等于")
		, NEQ("!=","不等于")
		, N("<>","不等于")
		, LIKE("like","等于")
		, NLIKE("not like","NOT LIKE")
		, IS("is","等于")
		, ISN("is not","IS NOT")
		, IN("in")
		, NIN("not in","NOT IN")
		, BETWEEN("between")
		, NBETWEEN("not between","NOT BETWEEN")
		, UNKOWN("","未知");
		
		private String name = "";
		private String desc = "";
		
		private MethodOPEAR(){
			
		}
		private MethodOPEAR(String name){
			this.name = name;
		}
		private MethodOPEAR(String name,String desc){
			this.name = name;
			this.desc = desc;
		}
		public String getName() {
			return name;
		}
		public String getDesc() {
			return desc;
		}
		public static MethodOPEAR get(String name){
			MethodOPEAR[] opears = values();
			for (MethodOPEAR opear : opears) {
				if(opear.getName().equalsIgnoreCase(name)){
					if(opear==EQ1){
						return MethodOPEAR.EQ;
					}
					return opear;
				}
			}
			return UNKOWN;
		}
	};


	protected MethodCondition(CONN conn, String name,  MethodOPEAR oper, Object value) {
		super(conn);
		this.name = name;
		this.value = value;
		this.opear = oper;
	}

	
	public static MethodCondition newInstance(String name, Object value){
		return new MethodCondition(CONN.AND, name,  MethodOPEAR.EQ, value);
	}
	
	
	public MethodCondition addParam(Object value){
		this.params.add(new KeyValue(value));
		return this;
	}

	public MethodCondition addParam(String key,Object value){
		this.params.add(new KeyValue(key, value));
		return this;
	} 
	
	public List<KeyValue> getParams() {
		return params;
	}
	public String getName() {
		return name;
	}

	public MethodCondition setName(String name) {
		this.name = name;
		return this;
	}

	public MethodOPEAR getOpear() {
		return opear;
	}

	/**
	 * 操作符
	 * @author ls
	 * 2014年11月22日下午4:06:58 
	 * @param opear
	 * @return
	 */
	public MethodCondition setOpear(MethodOPEAR opear) {
		this.opear = opear;
		return this;
	}


	/**
	 * 操作符
	 * @author ls
	 * 2014年11月22日下午4:06:48 
	 * @param opear
	 * @return
	 */
	public MethodCondition setOpear(String opear) {
		this.opear = MethodOPEAR.get(opear);
		return this;
	}
	
	
	@Override
	public String toString() {
		if (option != null) {
			return this.name + "(" + option + " " + SearchUtil.joiner(params, ",") + ")";
		}
		return this.name + "(" + SearchUtil.joiner(params, ",") + ")";
	}
	

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}

	
	public static void main(String[] args) {
		System.out.println(MethodCondition.newInstance("", "").getClass());
	}
}
